//
//  PrivacyTermsView.m
//  RentTable
//
//  Created by mac on 2024/12/3.
//

#import "PrivacyTermsView.h"

@interface PrivacyTermsView ()

@property (nonatomic, strong) UIButton *checkboxButton;
@property (nonatomic, strong) UILabel *termsLabel;


@end

@implementation PrivacyTermsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.isChecked = false;
        
        self.checkboxButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.checkboxButton setImage:[UIImage imageNamed:@"未勾选"] forState:UIControlStateNormal];
        [self.checkboxButton setImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateSelected];
        [self.checkboxButton addTarget:self action:@selector(toggleCheckbox) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.checkboxButton];
        [self.checkboxButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.leading.mas_equalTo(10);
            make.size.mas_equalTo(CGSizeMake(15, 15));
        }];
        
        self.termsLabel = [UILabel new];
        self.termsLabel.userInteractionEnabled = YES;
        [self addSubview:self.termsLabel];
        [self.termsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(-2);
            make.leading.equalTo(self.checkboxButton.mas_trailing).offset(12);
            make.height.mas_equalTo(20);
        }];
        
        
        NSString *termsText = @"我已阅读并接受《隐私条款》";
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:termsText];
        
        [attributedString addAttribute:NSForegroundColorAttributeName value:myRGBHex(0x666666) range:NSMakeRange(0, 7)];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont pingFangSCRegularWithSize:12] range:NSMakeRange(0, 7)];
        
        [attributedString addAttribute:NSForegroundColorAttributeName value:myRGBHex(0x333333) range:NSMakeRange(8, 5)];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont pingFangSCMediumWithSize:13] range:NSMakeRange(8, 5)];
        
        
        self.termsLabel.attributedText = attributedString;
        
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openPrivacyTerms)];
        [self.termsLabel addGestureRecognizer:tapGesture];
    }
    return self;
}


- (void)toggleCheckbox {
    self.isChecked = !self.isChecked;
    self.checkboxButton.selected = self.isChecked;
}


- (void)openPrivacyTerms {
    
    NSString *urlString = @"https://docs.qq.com/doc/DVmJiRmd5VXFIcmRI";
    NSURL *url = [NSURL URLWithString:urlString];
    
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    } else {
        NSLog(@"无法打开链接");
    }
}

@end
